package gsoc.ogsadai.model.diagram.part;

import gsoc.ogsadai.model.ModelPackage;
import gsoc.ogsadai.model.WorkflowType;
import gsoc.ogsadai.model.diagram.edit.parts.ActivityTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ActivityTypeInstanceNameEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.DataTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputStreamTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputStreamTypePipeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputsTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.OutputStreamTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.OutputsTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ParallelType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ParallelTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ParallelTypeWorkflowCompartmentEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineTypeActivityCompartment2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineTypeActivityCompartment3EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineTypeActivityCompartmentEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.SequenceType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.SequenceTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowType3EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel3EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel4EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel5EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel6EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabelEditPart;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class RequestVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "gsoc.ogsadai.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (WorkflowTypeEditPart.MODEL_ID.equals(view.getType())) {
				return WorkflowTypeEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				RequestDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return String.valueOf(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (ModelPackage.eINSTANCE.getWorkflowType().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((WorkflowType) domainElement)) {
			return WorkflowTypeEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry
				.getModelID(containerView);
		if (!WorkflowTypeEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (WorkflowTypeEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = WorkflowTypeEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case SequenceTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getWorkflowType().isSuperTypeOf(
					domainElement.eClass())) {
				return WorkflowType3EditPart.VISUAL_ID;
			}
			break;
		case ActivityTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getInputsType().isSuperTypeOf(
					domainElement.eClass())) {
				return InputsTypeEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getOutputsType().isSuperTypeOf(
					domainElement.eClass())) {
				return OutputsTypeEditPart.VISUAL_ID;
			}
			break;
		case InputsTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getInputType().isSuperTypeOf(
					domainElement.eClass())) {
				return InputTypeEditPart.VISUAL_ID;
			}
			break;
		case InputTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getInputStreamType().isSuperTypeOf(
					domainElement.eClass())) {
				return InputStreamTypeEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDataType().isSuperTypeOf(
					domainElement.eClass())) {
				return DataTypeEditPart.VISUAL_ID;
			}
			break;
		case OutputsTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getOutputStreamType().isSuperTypeOf(
					domainElement.eClass())) {
				return OutputStreamTypeEditPart.VISUAL_ID;
			}
			break;
		case WorkflowType2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getPipelineType().isSuperTypeOf(
					domainElement.eClass())) {
				return PipelineType2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getParallelType().isSuperTypeOf(
					domainElement.eClass())) {
				return ParallelType2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSequenceType().isSuperTypeOf(
					domainElement.eClass())) {
				return SequenceType2EditPart.VISUAL_ID;
			}
			break;
		case SequenceType2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getWorkflowType().isSuperTypeOf(
					domainElement.eClass())) {
				return WorkflowType3EditPart.VISUAL_ID;
			}
			break;
		case WorkflowType3EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getPipelineType().isSuperTypeOf(
					domainElement.eClass())) {
				return PipelineType2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getParallelType().isSuperTypeOf(
					domainElement.eClass())) {
				return ParallelType2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSequenceType().isSuperTypeOf(
					domainElement.eClass())) {
				return SequenceType2EditPart.VISUAL_ID;
			}
			break;
		case PipelineTypeActivityCompartmentEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getActivityType().isSuperTypeOf(
					domainElement.eClass())) {
				return ActivityTypeEditPart.VISUAL_ID;
			}
			break;
		case PipelineTypeActivityCompartment2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getWorkflowType().isSuperTypeOf(
					domainElement.eClass())) {
				return WorkflowType2EditPart.VISUAL_ID;
			}
			break;
		case PipelineTypeActivityCompartment3EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getActivityType().isSuperTypeOf(
					domainElement.eClass())) {
				return ActivityTypeEditPart.VISUAL_ID;
			}
			break;
		case ParallelTypeWorkflowCompartmentEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getWorkflowType().isSuperTypeOf(
					domainElement.eClass())) {
				return WorkflowType2EditPart.VISUAL_ID;
			}
			break;
		case WorkflowTypeEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getPipelineType().isSuperTypeOf(
					domainElement.eClass())) {
				return PipelineTypeEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getParallelType().isSuperTypeOf(
					domainElement.eClass())) {
				return ParallelTypeEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSequenceType().isSuperTypeOf(
					domainElement.eClass())) {
				return SequenceTypeEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry
				.getModelID(containerView);
		if (!WorkflowTypeEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (WorkflowTypeEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = WorkflowTypeEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case PipelineTypeEditPart.VISUAL_ID:
			if (WrappingLabelEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PipelineTypeActivityCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ParallelTypeEditPart.VISUAL_ID:
			if (WrappingLabel3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PipelineTypeActivityCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SequenceTypeEditPart.VISUAL_ID:
			if (WrappingLabel4EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (WorkflowType3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ActivityTypeEditPart.VISUAL_ID:
			if (ActivityTypeInstanceNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (InputsTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (OutputsTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InputsTypeEditPart.VISUAL_ID:
			if (InputTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InputTypeEditPart.VISUAL_ID:
			if (InputStreamTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DataTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InputStreamTypeEditPart.VISUAL_ID:
			if (InputStreamTypePipeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case OutputsTypeEditPart.VISUAL_ID:
			if (OutputStreamTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case WorkflowType2EditPart.VISUAL_ID:
			if (PipelineType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ParallelType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SequenceType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case PipelineType2EditPart.VISUAL_ID:
			if (WrappingLabel2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PipelineTypeActivityCompartment3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ParallelType2EditPart.VISUAL_ID:
			if (WrappingLabel5EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ParallelTypeWorkflowCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SequenceType2EditPart.VISUAL_ID:
			if (WrappingLabel6EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (WorkflowType3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case WorkflowType3EditPart.VISUAL_ID:
			if (PipelineType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ParallelType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SequenceType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case PipelineTypeActivityCompartmentEditPart.VISUAL_ID:
			if (ActivityTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case PipelineTypeActivityCompartment2EditPart.VISUAL_ID:
			if (WorkflowType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case PipelineTypeActivityCompartment3EditPart.VISUAL_ID:
			if (ActivityTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ParallelTypeWorkflowCompartmentEditPart.VISUAL_ID:
			if (WorkflowType2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case WorkflowTypeEditPart.VISUAL_ID:
			if (PipelineTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ParallelTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SequenceTypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(WorkflowType element) {
		return true;
	}

}
